package com.awesome.todo.activities;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.os.Bundle;
import android.util.Log;

import com.awesome.todo.Task;
import com.awesome.todo.TaskItemizedOverlay;
import com.awesome.todo.R;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;

public class TaskMapActivity extends MapActivity {

	MapView mapView;
	MapController mapController;
	List<Overlay> mapOverlays;
	Context context = this;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.taskmap);
		

        // TODO - get data for which Task object created this Activity
        // using getIntent().getBundle().getExtras(); -- google it. 
		String address = getIntent().getExtras().getString("address");
		String city = getIntent().getExtras().getString("city");
		String state = getIntent().getExtras().getString("state");
		Log.i("INFO", "Test: " + address + " " + city + " " + state);

		mapView = (MapView) findViewById(R.id.taskmapview);
		mapController = mapView.getController(); 
		mapOverlays = mapView.getOverlays(); // the set of overlays for this map.
                                             // To make an overlay appear on the map
                                             // just add it to our mapOverlays List.

		Geocoder coder = new Geocoder(context);
		GeoPoint taskGeoPoint = null;
		double latitude = 0;
		double longitude = 0;
		try {
			List<Address> geocodeAddress = coder.getFromLocationName(
					address + ", " + city + ", " + state, 5);
			if (geocodeAddress == null) {
				Log.e("ERROR", "Error getting geocoded address");
			}
			for (Address thisAddress : geocodeAddress) {
				Log.i("INFO", thisAddress.getAdminArea());
				try {
					latitude = thisAddress.getLatitude();
					longitude = thisAddress.getLongitude();
					break;
				} catch (IllegalStateException ie) {
					// go to the next one.
				}
					
			}
			Log.i("INFO", "Location: " + latitude + ", " + longitude);


			taskGeoPoint = new GeoPoint((int) (latitude * 1E6), (int) (longitude * 1E6));

		} catch (IOException e) {
			Log.e("ERROR", "SOMETHING BAD HAPPENED.");
		}
		
		GeoPoint centerPoint = new GeoPoint(33776499, -84398400);
		mapController.animateTo(centerPoint);
		mapController.setZoom(14);
    
        // The next three lines are an example for how to manually add a single overlay item to the Map View.    
		Drawable drawable = this.getResources().getDrawable(R.drawable.androidmarker);  // we'll change this to match our desired icon for pushpins 
		TaskItemizedOverlay itemizedoverlay = new TaskItemizedOverlay(drawable, this);  // this initializes a new TaskItemizedOverlay. Every item in

		OverlayItem overlayitem = new OverlayItem(taskGeoPoint, "Hello, world!", "I am here!"); // create a new OverlayItem

		itemizedoverlay.addOverlay(overlayitem); // add this OverlayItem to our custom TaskItemizedOverlay. replace "null" with task for overlay item.
		mapOverlays.add(itemizedoverlay); // add our TaskItemizedOverlay to the mapOverlays.
	}
	
	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}
	
	
}
